/******************************************************************
 * Created by: GOh CHee Hong                                      
 * Date of Creation: 1/Jan/2009            
 
 *                                                                
 * Description: This class is in charge of tracking a peer
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Pbdms.OpenDHTLib;
using Pbdms.Common;
using Pbdms.Common.CryptoUtils;
using System.Net;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
using System.Data;
using Pbdms.BusinessLogic.DataController;
using System.Drawing;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System.Timers;

namespace Pbdms.BusinessLogic.PeerMS
{
    public class PeerTracker
    {
        private string id;
        private Boolean trusted;
        public PollingManager pollManager;

        private IPAddress Ipaddress;
        private System.Timers.Timer mTimer = new System.Timers.Timer();
     
        public PeerTracker()
        {

        }
        ~PeerTracker()
        {
            if (mTimer != null)
            {
                mTimer.Elapsed -= new ElapsedEventHandler(OnTimer);
            }
        }
        public PeerTracker(string id,Boolean trusted,Network.NetworkController networkctlr)
        {
            this.id = id;
            this.trusted = trusted;
            pollManager = new PollingManager(this,networkctlr);
            mTimer.Elapsed += new ElapsedEventHandler(OnTimer);
            mTimer.Interval = 120000;

            mTimer.Enabled = true;
            
        }
        public string ID
        {
            get
            {
                return id;
            }
        }
        public Boolean getTrusted()
        {
            lock (this)
            {
                return trusted;
            }
        }
        public void setTrusted(Boolean trust)
        {
            lock (this)
            {
                trusted = trust;
                if (trust == false)
                {
                    Ipaddress = null;
                }
            }
            Thread start= new Thread(new ThreadStart(pollManager.polling));
            start.IsBackground=true;
            start.Start();
        }
        public void setIpAddress(IPAddress ip)
        {
            lock (this)
            {
                Ipaddress = ip;
            }
        }
        public IPAddress getIpAddress()
        {
            lock (this)
            {
                return Ipaddress;
            }
        }
        public void OnTimer(object source, ElapsedEventArgs e)
        {

            lock (this)
            {
                if (trusted == false)
                {
                    return;
                }
            }
            pollManager.polling();
        }
       





      
    }
}
